package ch.mueller.android.nmeaexample.util.readerFactoryElements;

import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.R.string;

public abstract class GPSLineAnalyser {
    
    public final static String CHECKSUM = "CheckSum";
    public final static String NMEATYPE = "Type";
    
    protected String line;
    
    GPSLineAnalyser(String line) {
        this.line = line;
    }

    public abstract LinkedHashMap<String, String> getResult();

    protected String calculateCheksum(String line) {
        String in = line.trim();
        int checksum = 0;
        if (in.startsWith("$")) {
            in = in.substring(1, in.length());
        }

        int end = in.indexOf('*');
        if (end == -1){
            end = in.length();
        }
        for (int i = 0; i < end; i++) {
            checksum = checksum ^ in.charAt(i);
        }
        String calculatedCS = Integer.toHexString(checksum);
        if (calculatedCS.length() == 1) {
            calculatedCS = "0" + calculatedCS;
        }
        String realCS = line.substring(in.length()-1, in.length()+1);
        if (realCS.equals(calculatedCS.toUpperCase())) {
            return calculatedCS.toUpperCase();
        }
        return "InCorrect: "+realCS+" != "+calculatedCS;
    }

    protected String getCheckSum(String line) {
        return line.substring(line.length() - 2, line.length());
    }

    protected String nsCkeck(String sORn) {
        sORn = sORn.trim();
        if (sORn.equals("N") || sORn.equals("S")) {
            return sORn;
        } else {
            return sORn + "Incorrect N/S";
        }
    }

    protected String owCkeck(String eORw) {
        eORw = eORw.trim();
        if (eORw.equals("E") || eORw.equals("W")) {
            return eORw;
        } else {
            return eORw + "Incorrect E/W";
        }

    }

    protected String avStatusCheck(String state) {
        state = state.trim();
        if (state.equals("A") || state.equals("V")) {
            return state;
        } else {
            return state + "Incorrect A/V";
        }

    }

    protected String UTCtimeCheck(String time) {
        time = time.trim();
        String fortattedTime = "";
        if (time.length() != 9) {
            return time + "Incorrect TimeFormat";
        }
        try {

            String hours = time.substring(0, 2);
            int h = Integer.parseInt(hours);
            if (h < 0 || h > 24) {
                return time + "Incorrect TimeFormat";
            }
            String minutes = time.substring(2, 4);
            int m = Integer.parseInt(minutes);
            if (m < 0 || m > 59) {
                return time + " Incorrect TimeFormat";
            }
            String seconds = time.substring(4, 6);
            int s = Integer.parseInt(seconds);
            if (s < 0 || s > 59) {
                return time + " Incorrect TimeFormat";
            }
            String milli = time.substring(7, 9);
            int mi = Integer.parseInt(milli);
            if (mi < 0 || mi > 999) {
                return time + "Incorrect TimeFormat";
            }
            fortattedTime = hours + "h" + minutes + "m" + seconds + "s" + milli + "ms";
        } catch (Exception e) {
            return time + "Incorrect TimeFormat";
        }
        return fortattedTime;
    }

    protected String rangeCheck(String val, int min, int max) {
        try {
            int nr = Integer.parseInt(val.trim());
            if (nr < min || nr > max) {
                return val + "Incorrect Range";
            }
        } catch (Exception e) {
        }

        return val;
    }

    protected String getValue(String[] splitts, int pos) {

        try {
            return " " + splitts[pos];
        } catch (Exception e) {
            return "Error";
        }
    }
}